<template>
    <!-- 搜索 -->
    <div class="search-box">
        <div class="flex">
            <div>
                <el-icon>
                    <Search />
                </el-icon><span>筛选搜索</span>
            </div>
            <div>
                <el-button>重置</el-button>
                <el-button type="primary" @click="search">查询搜索</el-button>
            </div>
        </div>
        <div style="padding-top: 20px">
            <el-form ref="searchFormRef" @submit.prevent="search">
                <el-form-item label="输入搜索:" prop="params.keyword" label-width="120px">
                    <el-input style="width: 200px" placeholder="品牌名称关键词" v-model="params.keyword"></el-input>
                </el-form-item>
            </el-form>
        </div>
    </div>
    <!-- 添加 -->
    <div class="add-box">
        <div class="data-list">
            <el-icon>
                <Tickets />
            </el-icon>
            <span>数据列表</span>
        </div>
        <el-button type="default" @click="router.push('/pms/updateBrand')">添加</el-button>
    </div>
    <el-table :data="tableData" border fit @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="id" label="编号"></el-table-column>
        <el-table-column prop="name" label="名称名称"></el-table-column>
        <el-table-column prop="firstLetter" label="品牌首字母">
        </el-table-column>
        <el-table-column prop="sort" label="排序"></el-table-column>
        <el-table-column label="品牌制造商">
            <template #default="scope" class="make">
                <el-switch v-model="scope.row.factoryStatus" :active-value="1" :inactive-value="0"
                    @change="updateFactoryStatusFn(scope.row)" />
            </template>
        </el-table-column>
        <el-table-column label="是否显示">
            <template #default="scope" class="make">
                <el-switch v-model="scope.row.showStatus" :active-value="1" :inactive-value="0"
                    @change="changeShowStatusFn(scope.row)" />
            </template>
        </el-table-column>
        <el-table-column label="相关">
            <template #default="scope" class="make">
                <span>
                    商品：
                    <span>{{ scope.row.productCommentCount }}</span>
                    评价：
                    <span>{{ scope.row.productCount }}</span>
                </span>
            </template>
        </el-table-column>
        <el-table-column label="操作" width="160">
            <template #default="scope">
                <el-button type="default" size="small" @click="editFn(scope.row.id)">编辑</el-button>
                <el-button type="danger" size="small" @click="delFn(scope.row.id)">删除</el-button>
            </template>
        </el-table-column>
    </el-table>

    <div class="bottom">
        <!-- 批量操作 -->
        <div>
            <el-select v-model="showBrandStatus" class="m-2" placeholder="批量操作" size="large">
                <el-option v-for="item in brandStatus" :key="item.showStatus" :label="item.text"
                    :value="item.showStatus" />
            </el-select>
            <el-button type="primary" @click="sureOperate">确定</el-button>
        </div>
        <!-- 分页 -->
        <ElConfigProvider :locale="zhCn">
            <el-pagination v-model:currentPage="pageNum" v-model:page-size="pageSize" background
                layout="total, sizes, prev, pager, next ,jumper" :total="total" @size-change="pageSizeChange"
                @current-change="pageNumChange" :page-sizes="[5, 10, 15]" />
        </ElConfigProvider>
    </div>


</template>
<script setup lang='ts'>
import { Tickets, Search } from '@element-plus/icons-vue';
import { useTable } from '@/hooks/useTable'
import { pro } from '@/api'
import zhCn from "element-plus/lib/locale/lang/zh-cn";
import { ElConfigProvider, ElMessage, ElMessageBox } from "element-plus";
import { reactive, ref } from 'vue';
import { useRouter, useRoute } from 'vue-router';
const router = useRouter()
const brandStatus = ref([{
    showStatus: 1,
    text: '显示品牌'
}, {
    showStatus: 0,
    text: '隐藏品牌'
}])
let showBrandStatus = ref<number>()
let idList = ref<number[]>([])
const params = reactive({
    keyword: '',
})
const { tableData, pageNum, pageSize, total, pageNumChange, pageSizeChange, search } = useTable(pro.getProBrandsApi, params);
// 更新品牌制造商状态
const updateFactoryStatusFn = (row: IProBrandsModel) => {
    pro.updateFactoryStatusApi({ ids: [row.id], factoryStatus: row.factoryStatus }).then(res => {
        if (res.code === 200) {
            ElMessage.success('修改成功')
        }
    }).catch(err => {
        row.factoryStatus === 1 ? 0 : 1
    })
}
// 封装是否显示品牌的请求
const updateBrandShowStatus = (ids: number[], showStatus: number) => {
    pro.uptBrandShowStatusApi({ ids, showStatus }).then(res => {
        if (res.code === 200) {
            ElMessage.success('修改成功')
        }
    }).catch(err => {
        showStatus === 1 ? 0 : 1
    })
}
// 是否显示
const changeShowStatusFn = (row: IProBrandsModel) => {
    updateBrandShowStatus([row.id], row.showStatus)
}
// 批量操作
const sureOperate = () => {
    updateBrandShowStatus(idList.value, showBrandStatus.value as number)
    search()
}
// 表格全选、单选
const handleSelectionChange = (data: IProBrandsModel[]) => {
    idList.value = data.map(item => item.id)
}
// 编辑
const editFn = (id: number) => {
    router.push(`/pms/updateBrand?id=${id}`)
}
// 删除
const delFn = (id: number) => { }
</script>
<style lang="less" scoped>
.search-box {
    padding: 20px;
    border: 1px solid #ccc;
    border-radius: 10px;
    margin: 20px 0;

    .flex {
        display: flex;
        justify-content: space-between;
    }
}

.add-box {
    padding: 15px;
    display: flex;
    justify-content: space-between;
    border: 1px solid #ebeef5;
    border-radius: 5px;
    align-items: center;
    margin-bottom: 20px;

    .data-list {
        display: flex;
        align-items: center;
    }
}

.bottom {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-top: 20px;

    .el-button {
        margin-left: 20px;
    }
}
</style>